package com.combetter.changxing.http.base;



import com.combetter.changxing.http.BaseBean;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/**
 * Author: qintao
 * E-mail: 954280376@qq.com
 * Date: 2018/3/22 11:30
 * Description: SmartSubscriber
 */

public abstract class HttpResult<T> implements Observer<BaseBean<T>> {

    BaseBean<T> tBaseBean;

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(BaseBean<T> tBaseBean) {
        this.tBaseBean = tBaseBean;
        //连接成功
        if (("0").equals(tBaseBean.getCode())) {
            onSuccess(tBaseBean.getData());
        } else {
            onFail(tBaseBean.getMsg());
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        if (e instanceof HttpException) {
            int httpCode = ((HttpException) e).response().code();
//            if (httpCode == 911 && AppUtils.isAppForeground()) { //登录超时，并且APP处于前台
//                ToastUtils.showShort("登录超时，请重新登陆");
//                HttpUtil.clearCookies();
//                XGPushManager.delAllAccount(Utils.getApp());
//                DrUserUtils.clearInfo();
//                CgUserUtils.clearInfo();
//                MfUserUtils.clearInfo();
//                ActivityUtils.finishAllActivities();
//                ActivityUtils.startActivity(MainActivity.class);
//                return;
//            }
        }
        onFail("请求失败，请稍后重试...");
    }

    @Override
    public void onComplete() {
    }


    protected abstract void onSuccess(T t);

    protected abstract void onFail(String message);

    protected BaseBean<T> getBaseBean() {
        return tBaseBean;
    }
}
